package offer.offer45;

/**
 * @Author: wjy
 * @Date: 2021/11/2 16:22
 */
public class Solution {

    public static void main(String[] args) {

        int[] ints = {10, 2};
        //int[] ints = {3,30,34,5,9};
        //int[] ints = {999999998,999999997,999999999};
        //int[] ints = {1, 1, 1};
        //int[] ints = {0,9,8,7,6,5,4,3,2,1};

/*        String str1 = "999999998";
        String str2 = "999999999";

        System.out.println(str1.compareTo(str2));*/

        Solution solution = new Solution();
        System.out.println(solution.minNumber(ints));

    }

    public String minNumber(int[] nums) {
        if (nums.length == 0) {
            return null;
        }

        String sum1 = null;
        String sum2 = null;
        int temp = 0;

        for (int i = 0; i < nums.length-1; i++) {
            for (int j = 1; j < nums.length-i; j++) {
                sum1 = String.valueOf(nums[j - 1]) + String.valueOf(nums[j]);
                sum2 = String.valueOf(nums[j]) + String.valueOf(nums[j - 1]);

                if (sum1.compareTo(sum2) > 0) {
                    temp = nums[j - 1];
                    nums[j - 1] = nums[j];
                    nums[j] = temp;
                }
            }
        }

        StringBuilder min = new StringBuilder();

        for (int i = 0; i < nums.length; i++) {
            min.append(nums[i]);
        }

        return min.toString();
    }

}
